<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="org.atf.utils.*,org.atf.model.entity.User" %>
<h2>Servers</h2>
<%
	User user = null;
	try {
				
		user = (User)session.getAttribute("ATFUser");
		
	}catch (Exception e){
		response.sendRedirect("login.jsp");
	}
	if( user == null){
			response.sendRedirect("login.jsp");
	}
%>

<div id="server-alert-container"></div>
<div class="row">
	<div class="col-md-8">
		<div class="panel panel-default">
			<div class="panel-heading">
		       <i class="fa fa-hdd-o"></i> <a data-toggle="collapse" href="#serverCollapse"> Server List </a> <a id="serverListRefresh"><i class="fa fa-refresh"></i></a>
		        <div class="pull-right">
                   <div class="btn-group">
                       <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                           Actions
                           <span class="caret"></span>
                       </button>
                       <ul class="dropdown-menu pull-right" role="menu">
                     	   <li><a  id="serverStatusBtn"><i class="fa fa-link fa-fw"></i>Status</a></li>
                           <li><a  id="serverStartBtn"><i class="fa fa-play fa-fw"></i>Start</a></li>
                           <li><a  id="serverStopBtn"><i class="fa fa-stop fa-fw"></i>Stop</a></li>
                           <li><a  id="serverRestartBtn"><i class="fa fa-repeat fa-fw"></i>Restart</a></li>                
                           <li><a  id="serverDeployBtn"><i class="fa fa-globe fa-fw"></i>Deploy</a></li>
                           <% if(user.getAdmin().equalsIgnoreCase("true")) { %>
                           <li><a  id="serverSettingBtn"><i class="fa fa-cog fa-fw"></i>Setting</a></li>
                           <%  } %>
                       </ul>
                   </div>
               </div>
		     </div>
		     <div id="serverCollapse" class="panel-collapse collapse in">
		    
			     <div class="panel-body">
			     <p>The List of the server across all the profiles you have access. You can select the server and do multiple action on it. 
			     	<br>Please select the options from the Actions dropdown</p>
			     	<div class="table-responsive">
			     		 <table class="table table-striped table-bordered table-hover" id="server-table-list">
			     		 	<thead>
			                    <tr>
			                    	<th>id</th>
			                        <th>Server Name</th>
			                        <th>Host Name</th>
			                        <th>Node Name</th>
			                        <th>Cell Name</th>
			                        <th>Status</th>
			                    </tr>
			                </thead>
			     		 </table>
			     	</div>
			     </div>
		     </div>
		</div>
		<div id="server-preference" ></div>
		<div id="task-output" ></div>
		<div id="server-deploy" ></div>
	</div>
	<div class="col-md-4">
		
		<div class="panel panel-default">
				<div class="panel-heading">
			       <i class="fa fa-tasks"></i> 
			       	<a data-toggle="collapse" href="#collapseTwo">
			       		Task Status
			       	</a>
			       	
			    </div>
			    <div id="collapseTwo" class="panel-collapse collapse in">
				    <div class="panel-body">
				    	<p>List of background tasks currently running.</p>
		    			<table id="taskStatusContainer" class="table" >
		    					<thead>
		    						<tr>
		    							<th></th>
		    							<th>Status</th>
		    							<th>Task</th>
		    							<th>Server Name</th>
		    							<th>Comments</th>
		    						</tr>
		    					
		    					</thead>
		    					<tbody>
			    					
		    					</tbody>
		    					
		    			</table>
	
				    </div>
			    </div>
		</div>
		<div class="panel panel-default">
			<div class="panel-heading">
		       <i class="fa fa-bell"></i> 
		       		<a data-toggle="collapse" href="#collapseOne"> Notification Panel </a>
		       <div class="pull-right">
                   <div class="btn-group">
                       <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                           Actions
                           <span class="caret"></span>
                       </button>
                       <ul class="dropdown-menu pull-right" role="menu">
                     	   <li><a  id="disableNotification"><i class="fa fa-stop fa-fw"></i>Disable</a></li>
                           <li><a  id="enableNotification"><i class="fa fa-play fa-fw"></i>Enable</a></li>
                           <li><a  id="clearNotification"><i class="fa fa-times fa-fw"></i>Clear</a></li>
                       </ul>
                   </div>
               </div>
		    </div>
		    <div id="collapseOne" class="panel-collapse collapse in">
			    <div class="panel-body">
			    	<div id="notificationPanelContainer" >
			    		
			    	</div>
			    </div>
		   </div>
		</div> <!--  End of Notification Panel -->
		
		<div class="panel panel-default">
			<div class="panel-heading">
		       <i class="fa fa-eye"></i> 
		       		<a data-toggle="collapse" href="#collapseThree"> Audit Panel </a>
		       		<!-- <div class="pull-right">
	                   <div class="btn-group">
	                       <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
	                           Actions
	                           <span class="caret"></span>
	                       </button>
	                       <ul class="dropdown-menu pull-right" role="menu">
	                     	   
	                       </ul>
	                   </div> 
               		</div>  -->
		    </div>
		    <div id="collapseThree" class="panel-collapse collapse in">
		   
			    <div class="panel-body">
			    	 <p>Last 5 task performed on the selectd server. This can used for auditing purpose</p>
			    		<div class="row">
			    			<div class="col-md-12">
			    				<div class="table-responsive">
			    				
			    					<table id="auditPanelTableContainer" class="table table-bordered table-striped" >
					    					<thead>
					    						<tr>
					    							<th>Task</th>
					    							<th>Time</th>
					    							<th>User</th>
					    							<th>Comments</th>
					    						</tr>
					    					</thead>
					    					<tbody>
						    					
					    					</tbody>
					    					
					    			</table>
					    			<p class="text-right"><small>-Last 5 records only</small></p>
			    				</div>
			    				
			    			</div>
			    		</div>
			    		
			    	
			    </div>
		   </div>
		</div> <!--  End of Notification Panel -->
		
	</div>
	
</div>



<script>
    $(document).ready(function() {
   		 uft.loadServerData();
   		 var selectedServer={};
   		 
        var serverTable= $('#server-table-list').dataTable( {
	        data: uft.serverData,
	        columns: [
	        	{ data: "id" ,"visible": false,},
	            { data: "servername" },
	            { data: "hostname" },
	            { data: "nodename" },
	            { data: "cellName" },
	            { data: "status", 
	              render: function ( data, type, row ) {
	                if(data == null){
	               		 data = "Unknown";
	                }
	            	if(data.toUpperCase() == "running".toUpperCase() || data.toUpperCase() == "started".toUpperCase()){
	            		return "<i class='fa fa-arrow-circle-right text-success' title='Started'></i>";
	            	}else if (data.toUpperCase() == "stopped".toUpperCase()) {
	            		return "<i class='fa fa-circle text-danger' title='Stopped'></i>";
	            	}else if (data.toUpperCase() == "scheduled".toUpperCase()) {
	            		return "<img src= 'images/time-add.png' title='Schedule'>";
	            	}else if(data.toUpperCase() == "starting".toUpperCase() || data.toUpperCase() == "stopping".toUpperCase()){
	            	 	return "<i class='fa fa-spinner' ></i>"
	            	}
	            	else{
	            		return "<i class='fa fa-question-circle text-warning' title='Unknown'></i>";
	            	}
                    
                } }
	           
	        ]
    	});
    $('#server-table-list tbody').on('click','tr', function () { 
    	  if(!$(this).hasClass('selected')){  
    	    selectedServer={};
    		$("#server-preference" ).empty();
    		$("#server-deploy" ).empty();
    		$("#server-table-list tbody tr").removeClass('selected');		
			$(this).addClass('selected');
    		var aPos = serverTable.fnGetPosition(this);
   		    var aData = serverTable.fnGetData(aPos);
   		    selectedServer=aData;
   		    uft.getAuditData(selectedServer.id)
    	  }
    		
   		   
    	 });
    <% if(user.getAdmin().equalsIgnoreCase("true")) { %>
    $( "#serverSettingBtn" ).click(function(){
        	if( !jQuery.isEmptyObject(selectedServer)){
        		var id =selectedServer.id;
        		$("#server-preference").load("jsp/preferences.jsp?serverId="+id)
        	}else{
    			$("#server-alert-container").atf_warn_flyover("Select a Server from the list", "oops!")
    		}
    })
    <% }%>
    $( "#serverDeployBtn" ).click(function(){
        	if( !jQuery.isEmptyObject(selectedServer)){
        		var id =selectedServer.id;
        		$("#server-deploy").load("jsp/deploy.jsp?serverId="+id)
        	}else{
    			$("#server-alert-container").atf_warn_flyover("Select a Server from the list", "oops!")
    		}
    })
    
    $( "#disableNotification" ).click(function(){
        	notificationUpdater.disableNotification();
        	uft.generateAlert("Notification Disabled","server-alert-container");
    })
    $( "#enableNotification" ).click(function(){
        	notificationUpdater.enableNotification();
        	uft.generateAlert("Notification Enabled","server-alert-container");
    })
     $( "#clearNotification" ).click(function(){
     	$("#notificationPanelContainer").empty()
     })
    
    
    
    
    $( "#serverStatusBtn" ).click(function(){
   	 
   	 	if( !jQuery.isEmptyObject(selectedServer)){
   	 		var id =selectedServer.id;
   	 		taskManager.serverStatus(id,{
			 async: false,
				callback:function(data){		
					if(data == "OK"){
					 data="SUCCESS: Schedule OK";				
					var taskObj ={taskId: "S"+selectedServer.id, serverId:selectedServer.id,  status: "Scheduled",  task: "STATUS", serverName:selectedServer.servername,comment:"Get Status Scheduled"}
					serverTaskStatusUpdater.NotifyAll(taskObj);
					uft.updateServerTaskStatus(taskObj);
					}
					uft.generateAlert(data,"server-alert-container");

				}
		 	}); 	
   		 }else{
    		$("#server-alert-container").atf_warn_flyover("Select a Server from the list", "oops!")
    	}
   
    });
    $( "#serverStartBtn" ).click(function(){
    	 if( !jQuery.isEmptyObject(selectedServer)){
    	 	var id =selectedServer.id;
    	 	taskManager.startServer(id,{
    	 		 async: false,
    	 		 callback:function(data){	
    	 		 	if(data == "OK"){
    	 		 		data="SUCCESS: Schedule OK";
    	 		 		var taskObj ={taskId: "S"+selectedServer.id, serverId:selectedServer.id,  status: "Scheduled",  task: "START", serverName:selectedServer.servername,comment:"Start Server Scheduled"}
						serverTaskStatusUpdater.NotifyAll(taskObj);	
						uft.updateServerTaskStatus(taskObj);
    	 		 	}
    	 		 	uft.generateAlert(data,"server-alert-container");
    	 		 }
    	 	})
    	 }else{
    		$("#server-alert-container").atf_warn_flyover("Select a Server from the list", "oops!")
    	}
    })
    $( "#serverStopBtn" ).click(function(){
    	if( !jQuery.isEmptyObject(selectedServer)){
    		var id =selectedServer.id;
    		taskManager.stopServer(id,{
    	 		 async: false,
    	 		 callback:function(data){	
    	 		 	if(data == "OK"){
    	 		 		data="SUCCESS: Schedule OK";
    	 		 		var taskObj ={taskId: "S"+selectedServer.id, serverId:selectedServer.id,  status: "Scheduled",  task: "STOP", serverName:selectedServer.servername,comment:"Stop Server Scheduled"}
						serverTaskStatusUpdater.NotifyAll(taskObj);	
						uft.updateServerTaskStatus(taskObj);
    	 		 	}
    	 		 	uft.generateAlert(data,"server-alert-container");
    	 		 }
    	 	})
    	}else{
    		$("#server-alert-container").atf_warn_flyover("Select a Server from the list", "oops!")
    	}
    })
    $( "#serverRestartBtn" ).click(function(){
    	if( !jQuery.isEmptyObject(selectedServer)){
    		var id =selectedServer.id;
    		taskManager.restartServer(id,{
    	 		 async: false,
    	 		 callback:function(data){	
    	 		 	if(data == "OK"){
    	 		 		data="SUCCESS: Schedule OK";
    	 		 		var taskObj ={taskId: "S"+selectedServer.id, serverId:selectedServer.id,  status: "Scheduled",  task: "RESTART", serverName:selectedServer.servername,comment:"Restart Server Scheduled"}
						serverTaskStatusUpdater.NotifyAll(taskObj);	
						uft.updateServerTaskStatus(taskObj);
    	 		 	}
    	 		 	uft.generateAlert(data,"server-alert-container");
    	 		 }
    	 	})
    	}else{
    	$("#server-alert-container").atf_warn_flyover("Select a Server from the list", "oops!")
    	}
    })
    
    $( "#serverListRefresh" ).click(function(){
    	serverTable.fnClearTable();
    	uft.serverData=[];
    	uft.loadServerData();
    	if(!jQuery.isEmptyObject(uft.serverData)){
    		serverTable.fnAddData(uft.serverData);
    	}
	   serverTable.fnDraw();
   
    });
    uft.loadServerTasks();
    
 })
</script>